package com.cornergj.back.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cornergj.model.Admin;
import com.cornergj.model.BranchAddress;
import com.cornergj.model.Entity;
import com.cornergj.model.Entity.AdminCriteria;
import com.cornergj.model.Entity.BranchAddressCriteria;
import com.cornergj.model.Entity.FrontImgCriteria;
import com.cornergj.model.Entity.Pagination;
import com.cornergj.model.Entity.ServiceContentCriteria;
import com.cornergj.model.Entity.SiteOfCityCriteria;
import com.cornergj.model.Entity.SiteOfProvinceCriteria;
import com.cornergj.model.Entity.Value;
import com.cornergj.model.FrontImg;
import com.cornergj.model.ServiceContent;
import com.cornergj.model.SiteOfCity;
import com.cornergj.model.SiteOfProvince;
import com.cornergj.response.ErrorCode;
import com.cornergj.response.JsonResult;
import com.cornergj.service.AdminService;
import com.cornergj.service.BranchAddressService;
import com.cornergj.service.FrontImgService;
import com.cornergj.service.ServiceContentService;
import com.cornergj.service.SiteOfCityService;
import com.cornergj.service.SiteOfProvinceService;
import com.cornergj.uitl.FileUtils;

/**
 * Title:
 * Model:
 * Description:
 * @author 郑耿杰
 * @date 2018年1月31日
 */
@Controller
@RequestMapping("/branchAddress")
public class BranchAddressController {
	
	@Autowired
	private BranchAddressService branchAddressService;
	
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, BranchAddress bean, Pagination pagination) {
		//查询条件封装
		BranchAddressCriteria criteria = new BranchAddressCriteria();
		//分页查询
		Pagination list = branchAddressService.selectPage(criteria, pagination);
		
		request.setAttribute("pagination", list);
		request.setAttribute("bean", bean);
		return "branchaddress/list";
	}
	/**
	 * 添加跳转
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(HttpServletRequest request) {
		return "branchaddress/choicemap";
	}
	/**
	 * 添加
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult save(HttpServletRequest request,BranchAddress branchAddress,String lnglat) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		try{
			String[] lnglatarray=lnglat.split(",");
			branchAddress.setLongitude(lnglatarray[0]);
			branchAddress.setLatitude(lnglatarray[1]);
			branchAddressService.save(branchAddress);
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.SYS_ERR);
		}
		return jsonResult;
	}
	/**
	 * 删除
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult delete(HttpServletRequest request,int branchid) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		try{
			branchAddressService.delete(branchid);
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.SYS_ERR);
		}
		return jsonResult;
	}
	/**
	 * 地图
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/viewmap", method = RequestMethod.GET)
	public String view(HttpServletRequest request,int branchid) {
		BranchAddress branchAddress=branchAddressService.selectOne(branchid);
		request.setAttribute("branchAddress", branchAddress);
		return "branchaddress/viewmap";
	}
}
